package com.threeox.commonlibrary.adapter.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.threeox.commonlibrary.util.ActivityUtils;
import com.threeox.utillibrary.util.EmptyUtils;
import com.threeox.utillibrary.util.ToastUtils;

import java.util.List;

/**
 *
 * @ClassName: CommonAdapter
 *
 * @Description: Todo(基础的Adapter)
 *
 * @author 赵屈犇
 *
 * @date 创建时间:2016/12/22 10:37
 *
 * @version 1.0
 */
@SuppressWarnings("rawtypes")
public abstract class CommonAdapter<T> extends BaseAdapter implements IAdapter<T> {

	protected final String TAG = this.getClass().getName();

	protected int mLayoutId;
	protected List<T> mDatas;
	protected Context mContext;
	protected LayoutInflater mInflater;
    protected boolean mScrollState = false;// 默认为不滚动状态

	public CommonAdapter() {
	}

	public CommonAdapter(Context context, List<T> datas) {
		this.init(context, datas);
	}

	public CommonAdapter(Context context, List<T> datas, Integer layoutId) {
        this.init(context, datas, layoutId);
	}

	public CommonAdapter init(Context context, List<T> datas) {
		return init(context, datas, null);
	}

	public CommonAdapter init(Context context, List<T> datas, Integer layoutId) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(context);
        this.mDatas = datas;
        if (layoutId != null) {
			this.mLayoutId = layoutId;
		}
        return this;
	}

	@Override
	public int getCount() {
		return mDatas.size();
	}

	@Override
	public T getItem(int position) {
		return mDatas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		BaseViewHolder holder = null;
		try {
			int viewType = getItemViewType(position);
			// 根据Item 设置布局
			setLayoutIdByViewType(viewType, getItem(position));
			if (beforeGetView(position, convertView, parent, holder)) {
				holder = BaseViewHolder.get(mContext, convertView, parent, mLayoutId, position);
			}
			conver(holder, position, viewType, getItem(position));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return holder.getConvertView();
	}

	/**
	 * 根据ViewType 设置布局
	 * 
	 * @param viewType
	 * @param item
	 */
	protected void setLayoutIdByViewType(int viewType, T item) {
	}

	/**
	 * 在getView 之前的操作
	 */
	protected boolean beforeGetView(int position, View convertView,
			ViewGroup parent, BaseViewHolder holder) {
		return true;
	};

	public abstract void conver(BaseViewHolder holder, int position, int viewType, T t);

	protected void startActivity(Class cls) {
		ActivityUtils.init(mContext, cls).start();
	}

	protected void showToast(String text) {
		ToastUtils.showLongToastSafe(mContext, text);
	}

	protected String getString(int resId) {
		return mContext.getString(resId);
	}

	/**
	 * 删除一列数据
	 * 
	 * @param position
	 */
	@Override
	public CommonAdapter remove(int position) {
		this.mDatas.remove(position);
		notifyDataSetChanged();
		return this;
	}
	
	@Override
	public CommonAdapter remove(T t) {
		if (t != null) {
			this.mDatas.remove(t);
			notifyDataSetChanged();
		}
		return this;
	}

	@Override
	public CommonAdapter removeAll() {
		if (mDatas != null) {
			this.mDatas.clear();
			notifyDataSetChanged();
		}
		return this;
	}

	@Override
	public CommonAdapter update(int pos, T t) {
		if (t != null) {
			this.mDatas.set(pos, t);
			notifyDataSetChanged();
		}
		return this;
	}

	/**
	 * 添加多条数据到第pos 位
	 *
	 * @param pos
	 * @param list
	 */
	@Override
	public CommonAdapter addAll(int pos, List<T> list) {
		if (EmptyUtils.isNotEmpty(list)) {
			this.mDatas.addAll(pos, list);
			notifyDataSetChanged();
		}
		return this;
	}

	@Override
	public CommonAdapter addAll(List<T> list) {
		if (EmptyUtils.isNotEmpty(list)) {
			this.mDatas.addAll(list);
			notifyDataSetChanged();
		}
		return this;
	}

	@Override
	public CommonAdapter add(int pos, T t) {
        if (EmptyUtils.isNotEmpty(t)) {
            this.mDatas.add(pos, t);
            notifyDataSetChanged();
        }
		return this;
	}

	@Override
	public CommonAdapter add(T t) {
		if (t != null) {
			this.mDatas.add(t);
			notifyDataSetChanged();
		}
		return this;
	}
	
	@Override
	public List<T> getDatas() {
		return this.mDatas;
	}

	@Override
	public void setScrollState(boolean scrollState) {
        this.mScrollState = scrollState;
	}

	public void setLayoutId(int layoutId) {
		this.mLayoutId = layoutId;
	}

	/*==============Activity和Fragment的生命周期==================*/

    @Override
    public void onCreate(Bundle savedInstanceState) {

    }

    @Override
    public boolean onStart() {
        return true;
    }

    @Override
    public boolean onRestart() {
        return true;
    }

    @Override
    public boolean onResume() {
        return true;
    }

    @Override
    public boolean onStop() {
        return true;
    }

    @Override
    public boolean onPause() {
        return true;
    }

    @Override
    public boolean onDestroy() {
        return true;
    }

    @Override
    public boolean onBackPressed() {
        return true;
    }

    @Override
    public void onAttach(Activity activity) {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return null;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

    }

    @Override
    public boolean onDestroyView() {
        return true;
    }

    @Override
    public boolean onDetach() {
        return true;
    }

    /*==============Activity和Fragment的生命周期==================*/

}
